package com.frank.loadinglibrary.common;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.app.Context;

/**
 * @author : frank
 * @date : 2021/4/29 16:05
 */
public abstract class BaseLoadingComponent<T extends BaseLoadingComponent> extends Component {

    //配置动画是否自动开启
    protected boolean autoStart = true;

    //配置开启透明度动画
    protected boolean enableAlphaAnimator = false;

    //配置动画时长
    protected int durtion = 1000;

    public void setDurtion(int durtion) {
        this.durtion = durtion;
    }

    public T setAutoStart(boolean autoStart) {
        this.autoStart = autoStart;
        return (T) this;
    }

    public T setEnableAlphaAnimator(boolean enableAlphaAnimator) {
        this.enableAlphaAnimator = enableAlphaAnimator;
        return (T) this;
    }

    public BaseLoadingComponent(Context context) {
        this(context, null);
    }

    public BaseLoadingComponent(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public BaseLoadingComponent(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
    }

    public BaseLoadingComponent(Context context, AttrSet attrSet, int resId) {
        super(context, attrSet, resId);
    }



    @Override
    public void setVisibility(int v) {
        if (getVisibility() != v) {
            super.setVisibility(v);
            if (v != VISIBLE) {
                stopAnimators();
            } else {
                if (autoStart) {
                    startAnimators();
                }
            }
        }
    }

    protected void show() {
        setVisibility(VISIBLE);
    }

    protected void hide() {
        setVisibility(HIDE);
    }

    //停止动画
    protected abstract void stopAnimators();

    //开始动画
    protected abstract void startAnimators();

    //释放资源
    protected void releaseSource() {
    }
}
